package com.dmatek.uwb.packet.ianalysis.handler;

import java.net.InetSocketAddress;
import java.util.Map;
import com.dmatek.uwb.packet.bean.TagInfor;
import com.dmatek.uwb.util.tool.AnalyticalUtil;

/**   
 * 心率上报的处理器
 * @author zhangfu  
 * @date 2019年12月3日 下午3:06:18 
 * @version V1.0   
 */
public class HeartRateReportHandler extends BaseAnalysisPackHandler {
	private TagReportHandler tagPackHandler;
	public HeartRateReportHandler(TagReportHandler tagPackHandler) {
		super();
		this.tagPackHandler = tagPackHandler;
	}
	@Override
	public boolean templateMethod(byte[] temps, int limit,
			InetSocketAddress sendAddress, int index) throws Exception {
		short cs = 0;
		String strRateId = "";
		if (temps[index + 1] == AnalyticalUtil.HEART_RATE_TYPE
				&& temps[index + AnalyticalUtil.HEART_RATE_PACK_SIZE - 1] == AnalyticalUtil.END) {
			for (int i = index, size = index + AnalyticalUtil.HEART_RATE_PACK_SIZE
					- 2; i < size; i++) {
				cs += temps[i];
			}
			cs &= 0x00FF;
			if ((byte) cs == temps[index + AnalyticalUtil.HEART_RATE_PACK_SIZE - 2]) {
				// 校验相同
				strRateId = String.format("%02X%02X", temps[index + 2], temps[index + 3]);
				// 获取所有的卡片资料
				 Map<String, TagInfor> tagsMap = null;
				if(null != tagPackHandler) {
					tagsMap = tagPackHandler.getTagsMap();
				}
				TagInfor tagInfor = null;
				if(null != tagsMap) {
					tagInfor = tagsMap.get(strRateId);
				}
				if (null != tagInfor) {
					tagInfor.setHeartRate((int) (temps[index + 5] & 0xFF));
				} else { // 此时卡片还没有上报，我们不用更新心率资料
					
				}
				return true;
			}
		}
		return false;
	}

	@Override
	public byte getPacketHeader() {
		// TODO Auto-generated method stub
		return AnalyticalUtil.HEAD;
	}
	@Override
	public int getPacketSize() {
		return AnalyticalUtil.HEART_RATE_PACK_SIZE;
	}
}
